package com.ruoyi.system.service.impl;

import cn.hutool.http.HttpRequest;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author：zhanghao
 * @name：WxService
 * @Date：2024/7/18 15:41
 */
@Service
public class WxService {

    @Autowired
    private ISysConfigService configService;

    public String sendTextMsg(String wxid, String text) {
        if (!(StringUtils.isNotEmpty(wxid) && StringUtils.isNotEmpty(text))) {
            return null;
        }
        String url = configService.selectConfigByKey("wx.sys.proxy.url") + "/api/sendTextMsg";
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("wxid", wxid);
        jsonObject.set("msg", text);
        Map<String, String> heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        System.out.println("sendTextMsg:url=" + url + ",wxid=" + wxid + ",text=" + text);
        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(60 * 1000).execute().body();
        System.out.println(result);
        return result;


    }
}
